package com.site.service.impl.dining;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.site.dao.dining.RestaurantDao;
import com.site.dao.dining.RestaurantPhotoDao;
import com.site.dto.dining.RestaurantBizBean;
import com.site.dto.dining.RestaurantPhotoBizBean;
import com.site.model.dining.RestaurantInfo;
import com.site.model.dining.RestaurantPhotoInfo;
import com.site.service.dining.RestaurantPhotoService;
import com.site.util.PropertyUtils;
import com.site.util.base.businessbean.FileBean;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;

/**
 * 餐厅相册Service
 * @author win7
 *
 */
@Service
public class RestaurantPhotoServiceImpl implements RestaurantPhotoService {

	private Log log=LogFactory.getLog(this.getClass());
	
	private RestaurantPhotoDao restaurantPhotoDao;
	
	private RestaurantDao restaurantDao;
	
	@Resource
	public void setRestaurantDao(RestaurantDao restaurantDao) {
		this.restaurantDao = restaurantDao;
	}

	@Resource
	public void setRestaurantPhotoDao(RestaurantPhotoDao restaurantPhotoDao) {
		this.restaurantPhotoDao = restaurantPhotoDao;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 主图增加
		if("MAINPHOTOADD".equals(command)){
			mainPhotoAdd((Map<String, Object>)obj);
		// 删除主图
		} else if(COMMAND_DELETE.equals(command)){
			mainPhotoDel((Map<String, Object>)obj);
		}
		return null;
	}

	/**
	 * 删除
	 * @param id
	 */
	private void mainPhotoDel(Map<String, Object> obj) {
		if(obj != null && !obj.isEmpty()){
			Long id = (Long)obj.get("id");
			RestaurantPhotoInfo restaurantPhotoInfo = restaurantPhotoDao.getEntityById(RestaurantPhotoInfo.class, id);
			if(null != restaurantPhotoInfo){
				HttpServletRequest request = (HttpServletRequest)obj.get("request");
				String path = request.getSession().getServletContext().getRealPath("/restaurant");
				File file = new File(path+restaurantPhotoInfo.getPath());
				if(null != file && file.exists()){
					file.delete();
				}
				log.info("餐厅主图  "+restaurantPhotoInfo.getPath()+" 正在删除…");
				restaurantPhotoDao.delete(RestaurantPhotoInfo.class, id);
			}
		}
	}

	/**
	 * 餐厅主图增加
	 * @param obj
	 */
	private void mainPhotoAdd(Map<String, Object> obj) {
		if(obj != null){
			RestaurantBizBean restaurantBizBean = (RestaurantBizBean)obj.get("restaurantBizBean");
			RestaurantInfo restaurantInfo = restaurantDao.getEntityById(RestaurantInfo.class, restaurantBizBean.getId());
			FileUploadHandle fileUploadHandle = (FileUploadHandle)obj.get("fileUploadHandle");
			if(null != fileUploadHandle){
				// 上传路径[主图]目录
				String mainPath = "/restaurant/"+restaurantInfo.getRno()+"/main";
				fileUploadHandle.setUploadDir(RestaurantServiceImpl.getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							RestaurantPhotoInfo restaurantPhotoInfo = new RestaurantPhotoInfo();
							restaurantPhotoInfo.setCreatedDate(new Date());
							restaurantPhotoInfo.setRid(restaurantBizBean.getId());
							restaurantPhotoInfo.setPath("/"+restaurantInfo.getRno()+"/main/"+fileBean.getNewName());
							restaurantPhotoDao.save(restaurantPhotoInfo);
							log.info("餐厅主图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
		}
	}

	@Override
	public List<RestaurantPhotoBizBean> getRestaurantMainPhotoById(Long rid) {
		 if(null != rid){
			// 查询主图 start 
			List<RestaurantPhotoBizBean> restaurantPhotoBizBeans = new ArrayList<RestaurantPhotoBizBean>();
			List<RestaurantPhotoInfo> restaurantPhotoInfos = restaurantPhotoDao.getRestaurantMainPhotoByRid(rid);
			if(null != restaurantPhotoInfos && !restaurantPhotoInfos.isEmpty()){
				for (RestaurantPhotoInfo restaurantPhotoInfo : restaurantPhotoInfos) {
					RestaurantPhotoBizBean restaurantPhotoBizBean = new RestaurantPhotoBizBean();
					PropertyUtils.copyProperties(restaurantPhotoBizBean, restaurantPhotoInfo);
					restaurantPhotoBizBeans.add(restaurantPhotoBizBean);
				}
			}
			// 查询主图 end
			return restaurantPhotoBizBeans;
		}
		return null;
	}
}
